package com.todo.biz.application.backend.service.impl;

import com.todo.biz.application.backend.service.CollectSetBackendService;
import com.todo.biz.application.convert.CollectConvert;
import com.todo.biz.application.entity.bo.CollectRelationBO;
import com.todo.biz.application.entity.bo.CollectSetBO;
import com.todo.biz.application.entity.condition.CollectSetConditionBO;
import com.todo.biz.application.service.CollectRelationService;
import com.todo.biz.application.service.CollectSetService;
import com.todo.biz.iface.backend.request.*;
import com.todo.biz.iface.backend.response.QueryCollectSetByConditionResp;
import com.todo.biz.iface.backend.response.QueryCollectSetDetailResp;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CollectSet)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("collectSetBackendService")
public class CollectSetBackendServiceImpl implements CollectSetBackendService {

    @Resource
    private CollectSetService collectSetService;
    @Resource
    private CollectRelationService collectRelationService;

    @Override
    public BizResponse<Boolean> insertCollectSet(InsertCollectSetReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("操作失败，数据异常");
        }
        CollectSetBO collectSetBO = CollectConvert.convertToCollectSetBO(req);
        Boolean inserted = collectSetService.insertCollectSet(collectSetBO);
        return BizResponse.success(inserted);
    }

    @Override
    public BizResponse<Boolean> updateCollectSetById(UpdateCollectSetByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("操作失败，数据异常");
        }
        CollectSetBO collectSetBO = CollectConvert.convertToCollectSetBO(req);
        return BizResponse.success(collectSetService.updateCollectSet(collectSetBO));
    }

    @Override
    public BizPageResponse<QueryCollectSetByConditionResp> queryCollectSetByCondition(QueryCollectSetByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            return BizPageResponse.operationFailed("操作失败，数据异常");
        }
        CollectSetConditionBO conditionBO = CollectConvert.convertToCollectSetConditionBO(req);
        PageResult<CollectSetBO> pageResult = collectSetService.queryCollectSetByCondition(conditionBO);
        return BizPageResponse.success(CollectConvert.convertToPageQueryCollectSetByConditionResp(pageResult));
    }

    @Override
    public BizResponse<Boolean> enableCollectSetById(EnableCollectSetByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("操作失败，数据异常");
        }
        Boolean enabled = collectSetService.enableCollectSetById(req.getId(), req.getEnable());
        return BizResponse.success(enabled);
    }

    @Override
    @Transactional
    public BizResponse<Boolean> updateCollectRelation(UpdateCollectRelationReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("操作失败，数据异常");
        }
        CollectSetBO collectSetBO = collectSetService.queryCollectSetById(req.getSetId());
        if (collectSetBO == null) {
            return BizResponse.operationFailed("操作失败，查询集合为空，无法操作");
        }
        if (EnableTypeEnum.ENABLE.equals(collectSetBO.getStatus())) {
            return BizResponse.operationFailed("操作失败，集合已启用，无法操作");
        }
        Boolean removed = collectRelationService.removeCollectRelationBySetId(req.getSetId());
        if (removed) {
            List<CollectRelationBO> collectRelationBOList = CollectConvert.convertToCollectRelationBOList(req);
            Boolean inserted = collectRelationService.batchInsertCollectRelation(collectRelationBOList);
            if (!inserted) {
                throw new BizException(BizErrorCodeEnum.OPERATION_FAILED);
            }
        }
        return BizResponse.success(true);
    }

    @Override
    public BizResponse<QueryCollectSetDetailResp> queryCollectSetDetail(QueryCollectSetDetailReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.operationFailed("操作失败，数据异常");
        }
        CollectSetBO collectSetBO = collectSetService.queryCollectSetById(req.getSetId());
        if (collectSetBO == null) {
            return BizResponse.success();
        }
        List<CollectRelationBO> collectRelationBOS = collectRelationService.queryCollectRelationBOBySetId(req.getSetId());
        QueryCollectSetDetailResp resp = CollectConvert.convertToQueryCollectSetDetailResp(collectSetBO, collectRelationBOS);
        return BizResponse.success(resp);
    }
}
